package com.blog.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.blog.entity.Article;
import com.blog.dao.ArticleDao;
import com.blog.myUtils.DateTime;
import com.blog.service.ArticleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.blog.service.BaseService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mars123
 * @since 2019-07-02
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Override
    public Article getArticleByCode(String code) {
        return new Article().selectOne(new EntityWrapper<Article>().eq("code", code));
    }

    @Override
    public Page<Article> index(Article param, Page<Article> page) {
        return new Article().selectPage(page, new EntityWrapper<Article>().orderBy("article.order"));
    }

    @Override
    public Article create(Article article) {
        article.setCreatedAt(DateTime.getTime());
        article.setUpdatedAt(article.getCreatedAt());
        if (article.insert()) {
            return article;
        }
        return null;
    }

    @Override
    public Article update(Article article) {
        article.setUpdatedAt(DateTime.getTime());
        if (article.updateById()) {
            return article;
        }
        return null;
    }

    @Override
    public boolean delete(int id) {
        return new Article().deleteById(id);
    }

    @Override
    public Article view(int id) {
        return new Article().selectById(id);
    }
}
